package com.unohacha.qqj.fzdr.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Base64;

import com.unohacha.qqj.fzdr.imp.MyApplication;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;


/**
 * Created by wanghongchuang
 * on 2016/8/25.
 * email:844285775@qq.com
 */
public  class Prefer {
    private static final String TAG = "Prefer";
    private static final String PREFERENCE_FILE = "fzdr";

    private static Prefer mInstance;
    private static SharedPreferences mPref;
    private static SharedPreferences.Editor editor;

    private final String KEY_TOKEN = "KEY_TOKEN";
    private final String KEY_PHONE = "KEY_PHONE";
    private final String KEY_NAME = "KEY_NAME";
    private final String KEY_L0GINTYPE = "KEY_LOGINTYPE";
    private final String KEY_IMAGE = "KEY_IMAGE";
    private final String KEY_REGTYPE = "KEY_REGTYPE";
    private final String KEY_NEEDGUIDE = "KEY_NEEDGUIDE";
    private final String KEY_PASSWORD = "KEY_PASSWORD";
    private final String KEY_lOGINTYPE = "KEY_lOGINTYPE";
    private final String KEY_USER_ID = "KEY_USER_ID";
    private final String KEY_CHATID = "KEY_CHAT";
    private final String KEY_NICK = "KEY_NICK";
    private final String KEY_MEMBER = "KEY_MEMBER";
    private final String KEY_XH = "KEY_XH";
    private final String KEY_EDIT_TYPE = "KEY_EDIT_TYPE";
    private final String KEY_EDIT_NATURE = "KEY_EDIT_NATURE";
    private final String KEY_EDIT_ELEMENT = "KEY_EDIT_ELEMENT";
    private final String KEY_EDIT_WEIGHT = "KEY_EDIT_WEIGHT";
    private final String KEY_EDIT_WIDTH = "KEY_EDIT_WIDTH";
    private final String KEY_OTHER_CONTENT = "KEY_OTHER_CONTENT";
    private final String KEY_SUPPLY_ID = "KEY_SUPPLY_ID";
    private final String KEY_MEMBER_THUMB = "KEY_MEMBER_THUMB";
    private final String KEY_ISCHECKED = "IS_CHECKED";
    private static final String KEY_AREA_ID = "KEY_AREA_ID";
    private final String KEY_FENXIAO = "IS_FENXIAO";//是否开启分销
    private final String KEY_JPUSHID = "KEY_JPUSHID";
    private final String KEY_PACK_UP_LABLE = "KEY_PACK_UP_LABLE";//保存选择的筛选标签
    private final String KEY_LOCATION = "KEY_LOCATION";//保存定位的经纬度
    private final static String KEY_PRO_CITY = "KEY_PRO_CITY";//保存省市
    private final static String KEY_PRO_REGION = "KEY_PRO_REGION";//保存区 高德地图定位到的
    private final static String KEY_RE_LOCATION = "KEY_RE_LOCATION";//保存区 id
    private final static String KEY_LOCATION_NAME = "KEY_LOCATION_NAME";//保存上一次切换的城市
    private final static String KEY_TYPE = "KEY_TYPE";//保存需求 供应
    private final static String KEY_ECOSPHERE = "KEY_ECOSPHERE";//纺织  织造 区分
    private final String KEY_PACK_UP_LOCATION = "KEY_PACK_UP_LOCATION";//保存选择的多选城市的标签
    private final String KEY_WXSUCFROM = "KEY_WXSUCFROM";
    
    public static Prefer getInstance() {
        if (null == mInstance) {
            mInstance = new Prefer();
        }
        return mInstance;
    }

    private Prefer() {
        mPref = MyApplication.getInstance().getSharedPreferences(PREFERENCE_FILE, Context.MODE_PRIVATE);
        editor = mPref.edit();
    }


    /**
     * 微信支付成功的类型
     */
    public void setWxSUCfrom(String WxSUCfrom) {
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_WXSUCFROM, WxSUCfrom);
        editor.commit();
    }

    public String getWxSUCfrom() {
        return mPref.getString(KEY_WXSUCFROM, "");
    }
    
    //token
    public void setToken(String token) {
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_TOKEN, token);
        editor.commit();
    }
    

    public String getToken() {
        return mPref.getString(KEY_TOKEN, "");
    }

    public void setNick(String nick){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_NICK, nick);
        editor.commit();
    }
    
    public String getNick(){
        return mPref.getString(KEY_NICK,"");
    }
    
    public void setMember_id(String member_id){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_MEMBER, member_id);
        editor.commit();
    }

    public String getMember_id(){
        return mPref.getString("KEY_MEMBER","");
    }
    
    public void setMember_thumb(String member_thumb){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_MEMBER_THUMB, member_thumb);
        editor.commit();
    }
    
    public String getMember_thumb(){
        return mPref.getString(KEY_MEMBER_THUMB,"");
    }
    //token
    public static void setLocationName(String locationName) {
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_LOCATION_NAME, locationName);
        editor.commit();
    }

    public String getLocationName() {
        return mPref.getString(KEY_LOCATION_NAME, "");
    }

    //需求 供应
    public static void setType(String type) {
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_TYPE, type);
        editor.commit();
    }

    public String getKeyType() {
        return mPref.getString(KEY_TYPE, "");
    }


    //纺织  织造区分
    public static void setEcosphere(int ecosphere) {
        SharedPreferences.Editor editor = mPref.edit();
        editor.putInt(KEY_ECOSPHERE, ecosphere);
        editor.commit();
    }

    public int getEcosphere() {
        return mPref.getInt(KEY_ECOSPHERE, 0);
    }


    //保存经纬度
    public  void saveLocation(Context context,String center){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_LOCATION, center);
        editor.commit();

    }

    public  String obtainLocation(Context context){
        return mPref.getString(KEY_LOCATION,"");
    }

    //保存定位到的城市
    public static void saveProCity(Context context,String center){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_PRO_CITY, center);
        editor.commit();

    }

    public static String obtainProCity(Context context){
        return mPref.getString(KEY_PRO_CITY,"");
    }

    //保存上回选择的包含在开放区的
    public static void saveReLoaction(Context context,String center){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_RE_LOCATION, center);
        editor.commit();

    }

    public static String obtainReLocation(Context context){
        return mPref.getString(KEY_RE_LOCATION,"");
    }


    //保存定位到的区域的城市
    public static void saveProRegion(Context context,String center){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_PRO_REGION, center);
        editor.commit();

    }
    
    //保存选择的区的id
    public static void saveAreaId(Context context,String center){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_AREA_ID, center);
        editor.commit();
    }
    
    public String getAreaId(){
        return mPref.getString(KEY_AREA_ID,"");
    }

    public static String obtainProRegion(Context context){
        return mPref.getString(KEY_PRO_REGION,"");
    }

    //token
    public void setPackUpLable(int  position) {
        SharedPreferences.Editor editor = mPref.edit();
        editor.putInt(KEY_PACK_UP_LABLE, position);
        editor.commit();
    }

    public int getPackUpLable() {
        return mPref.getInt(KEY_PACK_UP_LABLE, 0);
    }

    //token
    public void setPackUpLocation(String  position) {
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_PACK_UP_LOCATION, position);
        editor.commit();
    }

    public String getPackUpLocation() {
        return mPref.getString(KEY_PACK_UP_LOCATION, "");
    }
    
    //user_id
    public void setUser_id(String User_id) {
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_USER_ID, User_id);
        editor.commit();
    }

    public String getUser_id() {
        return mPref.getString(KEY_USER_ID, "");
    }

    //chatid
    public void setChatid(String chatid) {
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_CHATID, chatid);
        editor.commit();
    }

    public String getChatid() {
        return mPref.getString(KEY_CHATID, "");
    }


    //chatid
    public void setJPushID(String chatid) {
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_JPUSHID, chatid);
        editor.commit();
    }

    public String getJPushID() {
        return mPref.getString(KEY_JPUSHID, "");
    }


    //chatid
    public void setSwitBuCheckedFx(Boolean isChecked) {
        SharedPreferences.Editor editor = mPref.edit();
        editor.putBoolean(KEY_FENXIAO, isChecked);
        editor.commit();
    }

    public boolean getSwitBuCheckedFx() {
        return mPref.getBoolean(KEY_FENXIAO, false);
    }

    //chatid
    public void setSwitBuChecked(Boolean isChecked) {
        SharedPreferences.Editor editor = mPref.edit();
        editor.putBoolean(KEY_ISCHECKED, isChecked);
        editor.commit();
    }

    public boolean getSwitBuChecked() {
        return mPref.getBoolean(KEY_ISCHECKED, false);
    }


    //手机号
    public void setPhone(String phone) {
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_PHONE, phone);
        editor.commit();
    }

    public String getPhone() {
        return mPref.getString(KEY_PHONE, "");
    }

    //密码
    public void setPwd(String pwd) {
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_PASSWORD, pwd);
        editor.commit();
    }

    public String getPwd() {
        return mPref.getString(KEY_PASSWORD, "");
    }


    //名称
    public void setName(String name) {
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_NAME, name);
        editor.commit();
    }

    public String getName() {
        return mPref.getString(KEY_NAME, "");
    }

    //登录类型（密码or短信）
    public void setloginTypeS(String name) {
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_lOGINTYPE, name);
        editor.commit();
    }

    public String getloginTypeS() {
        return mPref.getString(KEY_lOGINTYPE, "");
    }

    //登录类型  1 手机号  2 微信  3qq
    public void setLoginType(String loginTypoe) {
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_L0GINTYPE, loginTypoe);
        editor.commit();
    }

    public String getLoginType() {
        return mPref.getString(KEY_L0GINTYPE, "");
    }


    public String getImage() {
        return mPref.getString(KEY_IMAGE, "");
    }

    public void setImage(String profileImage) {
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_IMAGE, profileImage);
        editor.commit();
    }


    public void setRegType(String regType) {
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_REGTYPE, regType);
        editor.commit();
    }

    //登录方式
    public String getRegType() {
        return mPref.getString(KEY_REGTYPE, "");
    }


    //引导页

    public void setNeedGuide(boolean needGuide) {
        SharedPreferences.Editor editor = mPref.edit();
        editor.putBoolean(KEY_NEEDGUIDE, needGuide);
        editor.commit();
    }

    public boolean getneedGuide() {
        return mPref.getBoolean(KEY_NEEDGUIDE, true);
    }
    
    public void setEditXH(String xh){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_XH, xh);
        editor.commit();
    }
    
    public String getEditXH(){
        return mPref.getString(KEY_XH,"");
    }
    
    public void setEditType(String xh){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_EDIT_TYPE, xh);
        editor.commit();
    }
    
    public String getEditType(){
        return mPref.getString(KEY_EDIT_TYPE,"");
    }
    
    public void setEditNature(String xh){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_EDIT_NATURE, xh);
        editor.commit();
    }
    
    public String getEditNature(){
        return mPref.getString(KEY_EDIT_NATURE,"");
    }
    
    public void setEditElement(String xh){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_EDIT_ELEMENT, xh);
        editor.commit();
    }
    
    public String getEditElement(){
        return mPref.getString(KEY_EDIT_ELEMENT,"");
    }
    
    public void setEditWeight(String xh){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_EDIT_WEIGHT, xh);
        editor.commit();
    }
    
    public String getEditWeight(){
        return mPref.getString(KEY_EDIT_WEIGHT,"");
    }
    
    public void setEditWidth(String xh){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_EDIT_WIDTH, xh);
        editor.commit();
    }
    
    public String getEditWidth(){
        return mPref.getString(KEY_EDIT_WIDTH,"");
    }
    
    public void setOtherContent(String xh){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_OTHER_CONTENT, xh);
        editor.commit();
    }
    
    public String getOtherContent(){
        return mPref.getString(KEY_OTHER_CONTENT,"");
    }
    
    public void supply_id(String supply_id){
        SharedPreferences.Editor editor = mPref.edit();
        editor.putString(KEY_SUPPLY_ID, supply_id);
        editor.commit();
    }
    
    public String getSupply_id(){
        return mPref.getString(KEY_SUPPLY_ID,"");
    }


    public void putObject(String key, Object object) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream out = null;
        try {
            out = new ObjectOutputStream(baos);
            out.writeObject(object);
            String objectVal = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
            editor.putString(key, objectVal);
            editor.commit();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public <T> T getObject(String key, Class<T> clazz) {
        if (mPref.contains(key)) {
            String objectVal = mPref.getString(key, null);
            byte[] buffer = Base64.decode(objectVal, Base64.DEFAULT);
            ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
            ObjectInputStream ois = null;
            try {
                ois = new ObjectInputStream(bais);
                T t = (T) ois.readObject();
                return t;
            } catch (StreamCorruptedException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bais != null) {
                        bais.close();
                    }
                    if (ois != null) {
                        ois.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

    //退出登录后清除缓存数据
    public void clearData() {
        boolean needguide = getneedGuide();

        //清楚数据
        SharedPreferences.Editor editor = mPref.edit();
        editor.clear();
        editor.commit();

        //重新写入
        setNeedGuide(needguide);
    }
}
